package chasegame;

import game.Constants;
import game.GameMap;
import game.TwistyBasicGameState;

import java.util.ArrayList;

import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.StateBasedGame;

import vision.Vision;

/**
 * Chase Game
 * @author Twisty
 *
 */
public class ChaseGame extends TwistyBasicGameState {
	// A list of chasers.
	ArrayList<Chaser> chasers;
	
	// Initialize step number.
	long step = 0;
	
	// Image used for chasers.
	Image chaserImage;
	
	// Background image
	Image bg1;

	/**
	 * Class constructor.
	 * @param vision	Vision object used for recognizing changes in gamera.
	 */
	public ChaseGame(Vision vision) {
		super(vision);
	}

	@Override
	public void render(GameContainer arg0, StateBasedGame game, Graphics g) throws SlickException {
		super.render(arg0, game, g);
		g.setBackground(new Color(153,217,234));
		g.drawImage(bg1, ((Constants.screenWidth/2)-(bg1.getWidth()/2)), ((Constants.screenHeight/2)-(bg1.getHeight()/2)));
		
		for(Chaser c: chasers){
			g.rotate((float) c.getX()+16,(float) c.getY()+16,(float) c.getAngle());
			g.drawImage(chaserImage, c.getX(), c.getY());
			g.resetTransform();
		}
		
		for(int x = 0; x < Constants.screenWidth; x++){
			for(int y = 0; y < Constants.screenHeight; y++){
				if(this.map.hasPointAtLocation(x, y)){
					g.drawRect(x, y, 1, 1);
				}
			}
		}
	}

	@Override
	public void init(GameContainer arg0, StateBasedGame arg1) throws SlickException {
		chasers = new ArrayList<Chaser>();
		for(int i = 0; i < 15; i++){
			int rX = ((int) (Math.random()*Constants.screenWidth));
			int rY = ((int) (Math.random()*Constants.screenHeight));
			//Make sure they stay in the map width and height
			rX = rX -32;
			rY = rY -32;
			if(rX < 0) rX = 0;
			if(rY < 0) rY = 0;
			chasers.add(new Chaser(rX,rY));
		}
		chaserImage = new Image("res/frog.png");
		bg1 = new Image("res/bg1.png");
		map = new GameMap(Constants.screenWidth, Constants.screenHeight);
		
	}
	
	public void drawDotsOnMap(int dotAmount){
		for(int i = 0; i < dotAmount; i++){
			int x = ((int) (Math.random()*Constants.screenWidth));
			int y = ((int) (Math.random()*Constants.screenHeight));
			for(int xx = x; xx < x+24; xx++){
				for(int yy = y; yy < y+24; yy++){
					map.setPointOnMap(xx,yy,1);
				}
			}
		}
	}

	@Override
	public void update(GameContainer arg0, StateBasedGame game, int arg1) throws SlickException {
		super.update(arg0, game, arg1);
		
		for(Chaser c: chasers){
			c.collisionCheck(chasers);
			c.chase(map);
			c.move(map);
		}
		step++;
	}

	@Override
	public int getID() {
		// TODO Auto-generated method stub
		return Constants.chaseGame;
	}
}
